package cn.shop.dys.util;

import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;

import com.lidroid.xutils.DbUtils;

import java.util.List;

import cn.shop.dys.bean.CartProInDB;
import cn.shop.dys.bean.ProductProperty;
import cn.shop.dys.dao.CartDao;

/**
 * Created by ZhangJun 2016/4/23 18:33
 */
public class ComUtil {
    /**
     * 跳转到activity 或者service界面，
     * 类名不含有activity 或者service，不规范就出错，
     * @param context
     * @param clazz
     */
    public static void startActivityOrService(Context context, Class<? extends ContextWrapper> clazz) {
        Intent intent = new Intent(context, clazz);
        if (clazz.getSimpleName().contains("Activity")) {
            context.startActivity(intent);
        } else {
            context.startService(intent);
        }
    }

    public static boolean proToCartDB(
            String userId, int proId, int proNum, List<ProductProperty> productPropertys) {
        String text = propropertyListToText(productPropertys);
        CartProInDB mCartProInDB = new CartProInDB(userId, proId, proNum, text);
        boolean result = CartDao.add(mCartProInDB);
        return result;
    }
    public static boolean proToCartDB(
            String userId, int proId, int proNum, String productPropertys) {

        CartProInDB mCartProInDB = new CartProInDB(userId, proId, proNum, productPropertys);
        boolean result = CartDao.add(mCartProInDB);
        return result;
    }



    public static String propropertyListToText(List<ProductProperty> productPropertys) {
        String propropertyText = new String();
        if (productPropertys != null && productPropertys.size() > 0) {
            for (ProductProperty productProperty : productPropertys) {
                propropertyText += productProperty.getId() + ",";
            }
            propropertyText = propropertyText.substring(0, propropertyText.length() - 1);

        }
        return propropertyText;
    }
}
